package com.jmemoir.base.common.config;

import com.jmemoir.base.common.interceptor.CustomHandlerInterceptor;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ResourceUtils;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.io.FileNotFoundException;

/**
 * Web Mvc 配置
 *
 * @author Tellsea
 * @date 2023/8/2
 */
@Configuration
@RequiredArgsConstructor
public class WebMvcConfig implements WebMvcConfigurer {

    private final CustomHandlerInterceptor customHandlerInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(customHandlerInterceptor).addPathPatterns("/**");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        try {
            // 把项目根目录的 uploadPath 文件夹设置为静态资源根目录
//            http://localhost:8080/uploadPath/wx-tellsea.jpg
            String projectPath = ResourceUtils.getFile("").getAbsolutePath();
            registry.addResourceHandler("/uploadPath/**")
                    .addResourceLocations("file:" + projectPath + "/uploadPath/");
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
}